import java.util.ArrayList;
import java.util.List;

public class BinaryTree {
    public int val;
    public class TreeNode{
        public char val;//节点的值
        public TreeNode left;//节点的左孩子
        public TreeNode right;//节点的右孩子

        public TreeNode(char val) {
            this.val = val;
        }
    }
    //手动创建二叉树
    public TreeNode CreateTree(){
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        TreeNode G = new TreeNode('G');
        TreeNode H = new TreeNode('H');
        A.left = B;
        A.right = C;
        B.left = D;
        B.right = E;
        C.left = F;
        C.right = G;
        E.right = H;
        return A;
    }
    //对二叉树进行遍历：1.前序遍历2.中序遍历3.后序遍历4.层序遍历
    //前序遍历
    public void preOrder(TreeNode root){
        if (root == null){
            return;
        }
        System.out.print(root.val+" ");
        preOrder(root.left);
        preOrder(root.right);
    }
    //带有返回值的前序遍历1
//    List<Character> ret = new ArrayList<>();
//    public List<Character> preorderTraversal(TreeNode root) {
//        if (root == null){
//            return ret;
//        }
//        ret.add(root.val);
//        preOrder(root.left);
//        preOrder(root.right);
//        return ret;
//    }
    //带有返回值的前序遍历2
    public List<Character> preorderTraversal(TreeNode root) {
        List<Character> ret = new ArrayList<>();
        if (root == null){
            return ret;
        }
        ret.add(root.val);
        List<Character> leftTree = new ArrayList<>();
        ret.addAll(leftTree);
        List<Character> rightTree = new ArrayList<>();
        ret.addAll(rightTree);
        return ret;
    }
    //中序遍历
    public void inOrder(TreeNode root){
        if (root == null){
            return;
        }
        inOrder(root.left);
        System.out.print(root.val+" ");
        inOrder(root.right);
    }
    //后后序遍历
    public void postOrder(TreeNode root){
        if (root == null){
            return;
        }
        postOrder(root.left);
        postOrder(root.right);
        System.out.print(root.val+" ");
    }
    // 获取树中节点的个数
    public int num = 0;
    public int size(TreeNode root){
        //1.思路1:root!=null时节点个数加1
//        if (root == null){
//            return 0;
//        }
//        num++;
//        size(root.left);
//        size(root.right);
//        return num;
        //2.思路2:节点的总数等于左树节点+右树节点+1
        if (root == null){
            return 0;
        }
        int ret = size(root.left)+size(root.right)+1;
        return ret;
    }
    // 获取叶子节点的个数
    public static int leaf = 0;
    public int getLeafNodeCount(TreeNode root){
        //思路1：当root的左节点和右节点都为空的时候，叶子节点个数+1
//        if (root == null){
//            return 0;
//        }else if (root.left == null && root.right == null){
//            leaf++;
//        }
//        getLeafNodeCount(root.left);
//        getLeafNodeCount(root.right);
//        return leaf;
        //思路2：左树的叶子节点数+右树的叶子节点数就是整棵二叉树的叶子节点的个数
        if (root == null){
            return 0;
        }
        if (root.left == null && root.right == null){
            return 1;
        }
        return getLeafNodeCount(root.left) + getLeafNodeCount(root.right);
    }
}
